<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesData" border>
        <!-- 角色列表表格 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 一级权限 -->
            <el-row
              v-for="(item1,index1) in scope.row.children"
              :class="['bdBottom',index1 == 0 ? 'bdTop' : '','vertical']"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable @close="removeRightsTag(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 二级权限 -->
                <el-row
                  v-for="(item2,index2) in item1.children"
                  :key="item2.id"
                  :class="[index2 !== 0 ? 'bdTop' :'','vertical']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightsTag(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 三级权限 -->
                    <el-tag
                      type="warning"
                      v-for="(item3,index3) in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightsTag(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showDialogRight(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="rightsDialogVisible"
      width="50%"
      @close="setRightDialogClosed"
    >
      <!-- 分配权限树形结构 -->
      <el-tree
        :data="rightsData"
        :props="rightsProps"
        show-checkbox
        default-expand-all
        :default-checked-keys="defkeys"
        node-key="id"
        ref="treeRefs"
      ></el-tree>
      <!-- 分配权限对话框底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  name: "RolesList",
  data() {
    return {
      // 角色列表数据
      rolesData: [],
      //分配权限对话框隐藏于显示属性
      rightsDialogVisible: false,
      //权限列表数据
      rightsData: [],
      //角色ID
      roleId: "",
      rightsProps: {
        children: "children",
        label: "authName"
      },
      //默认分配权限树形选中数组
      defkeys: []
    };
  },
  created() {
    //打开页面获取用户列表方法
    this.getRolesList();
  },
  methods: {
    //获取用户列表方法
    async getRolesList() {
      const { data: res } = await this.$http.get("roles");
      if (res.meta.status != 200)
        return this.$message.error("获取角色列表失败");
      this.rolesData = res.data;
    },
    //删除权限标签方法
    async removeRightsTag(roles, rights) {
      const rightsTagConfirm = await this.$confirm(
        "此操作将永久删除该角色权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => error);
      if (rightsTagConfirm != "confirm") {
        return this.$message.info("取消了删除角色权限");
      }

      const { data: res } = await this.$http.delete(
        `roles/${roles.id}/rights/${rights}`
      );
      if (res.meta.status != 200) {
        this.$message.error("删除角色权限失败");
      }

      roles.children = res.data;

      this.$message.success("删除角色权限成功");
    },
    //显示分配权限对话框方法
    async showDialogRight(role) {
      this.roleId = role.id;
      const { data: res } = await this.$http.get("rights/tree");
      this.rightsData = res.data;
      if (res.meta.status != 200) {
        this.$message.error("获取权限列表失败");
      }
      //默认设置树形分配权限的默认选择
      this.getLeafKeys(role, this.defkeys);
      this.rightsDialogVisible = true;
    },

    // 获取三级权限的ID方法
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr);
      });
    },
    //关闭分配权限对话框
    //清空默认分配权限树形选中数组(this.defkeys)
    setRightDialogClosed() {
      this.defkeys = [];
    },
    //分配角色给权限方法
    async allotRights() {
      const strId = [
        //获取全选中节点的Id
           ...this.$refs.treeRefs.getCheckedKeys(),
        //获取半选中节点的Id
           ...this.$refs.treeRefs.getHalfCheckedKeys()
      ];
      //发送更新权限列表AJAX请求
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        {
          rids: strId.join(",")
        }
      );
       if(res.meta.status != 200){
          return this.$message.error("更新角色权限失败")
       }
       //更新角色列表
       this.getRolesList();       

      this.rightsDialogVisible = false;
    }
  }
};
</script>

<style lang="less" scoped>
.vertical {
  display: flex;
  align-items: center;
}
</style>